/* Copyright (C) 2008, 2009, 2010 Faindu GmbH, Switzerland.
* All Rights Reserved. The following is Source Code and is subject to all restrictions 
* on such code as contained in the license accompanying this project. */
package blocstac.editimage.base
{
	import blocstac.api.blocbox.ImageLoader;
	import blocstac.api.blocbox.MimeType;
	import blocstac.editimage.EditImageData;
	import blocstac.util.CreateLogger;
	
	import com.adobe.images.PNGEncoder;
	
	import flash.display.BitmapData;
	import flash.utils.ByteArray;
	
	import mx.logging.ILogger;

public class EditImageDataImpl implements EditImageData
{
	
	private static const LOG:ILogger = CreateLogger.withClassNameOf(EditImageDataImpl);
	
	//--------------------------------------------------------------------------
	//
	//  Properties
	//
	//--------------------------------------------------------------------------
	//----------------------------------
	//  imageLoader
	//----------------------------------	
	private var _imageLoader:ImageLoader
	/** @inheritDoc */
	public function get imageLoader():ImageLoader {
		return _imageLoader
	}
	
	//----------------------------------
	//  config
	//----------------------------------	
	private var _angle:Number = 0
	private var _scaleX:Number = 1
	private var _scaleY:Number = 1
	/** @inheritDoc */
	public function get config():String {
		if (_angle != 0 || _scaleX != 0 || _scaleY != 0) {
			return "<editimage><angle>" + _angle + "</angle><scaleX>" + _scaleX + "</scaleX><scaleY>" + _scaleY + "</scaleY></editimage>"
		}
		return null
	}
	public function set config(value:String):void {
		if (value) {
			try {
				var newConfig:XML = XML(value)
				if (newConfig) {
					_angle = newConfig.angle
					_scaleX = newConfig.scaleX
					_scaleY = newConfig.scaleY
					return
				} else {
					LOG.warn("newConfig is null")
				}
			} catch (e:TypeError) {
				LOG.warn("value is not valid XML", e)
			}
		} 
		_angle = 0
		_scaleX = 1
		_scaleY = 1
	}
	
	//----------------------------------
	//  mimeType
	//----------------------------------
	private var _mimeType:MimeType
	/** @inheritDoc */
	public function get mimeType():MimeType {
		return _mimeType
	}
	public function set mimeType(value:MimeType):void {
		_mimeType = value
	}

	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------		
	internal function createFrom(imageModel:ImageModel, imageLoader:ImageLoader):void {
		_mimeType = imageModel.mimeType
		
		var bytes:ByteArray = imageModel.bytes
		if (! bytes && imageModel.imageDataUpdateIndex > 1) {
			if (imageModel.mimeType.toString() == MimeType.MIMETYPE_PNG) {
				bytes = PNGEncoder.encode(imageModel.imageData)
				_mimeType = new MimeType(MimeType.MIMETYPE_PNG)
			} else if (imageModel.mimeType.toString() == MimeType.MIMETYPE_GIF) {
				bytes = PNGEncoder.encode(imageModel.imageData)
				_mimeType = new MimeType(MimeType.MIMETYPE_PNG)
			} else {
				bytes = createJpgImage(imageModel.imageData)
				_mimeType = new MimeType(MimeType.MIMETYPE_JPG)
			}
		}
		ImageLoaderImpl(imageLoader).setBytes(bytes, _mimeType)
		_imageLoader = imageLoader
		
		_angle = imageModel.angle
		_scaleX = imageModel.editedScaleX
		_scaleY = imageModel.editedScaleY
	}
	
	internal function copyTo(state:ImageModel):void {
		state.angle = _angle
		state.editedScaleX = _scaleX
		state.editedScaleY = _scaleY
		state.mimeType = _mimeType
	}
	
	private function createJpgImage(imageData:BitmapData):ByteArray {
		var jpgEncoder:JpgEncoder = new JpgEncoder(80)
		return jpgEncoder.encode(imageData)
	}

}
}